<template>
    <div class="app-container">
        <div class="customContainer ">
            <!-- 搜索头 -->
            <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
                <el-form-item>
                    <el-date-picker v-model="pickerDate" value-format="yyyy.MM.dd" type="daterange" align="right"
                        unlink-panels range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :picker-options="pickerOptions"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-select filterable placeholder="选择门店" v-model="formInline.store_id">
                        <el-option v-for="item in storeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <dj-button size="small" @search="searchClick" />
                </el-form-item>
            </el-form>
            <!-- 表格 -->
            <el-table  highlight-current-row :data="list" v-loading.body="listLoading" border size="small">
                <el-table-column align="center" prop="voucher_name" label="代金券名称"></el-table-column>
                <el-table-column align="center" prop="limit_money" label="代金券金额"></el-table-column>
                <el-table-column align="center" prop="discount_money" label="抵扣金额"></el-table-column>
                <el-table-column align="center" prop="use_num" label="使用张数"></el-table-column>
                <el-table-column align="center" prop="create_time" label="使用时间"></el-table-column>
                <el-table-column align="center" prop="emp_name" label="操作员工"></el-table-column>
            </el-table>
        </div>
        <!-- 自定义底部 -->
        <div class="bottom_box">
            <el-pagination class="pull-left" background @current-change="handleCurrentChange" :current-page.sync="currentPage"
                :page-size="20" layout="prev, pager, next, jumper" :total="totalDataNumber">
            </el-pagination>
            <span style="margin-top:10px" class="pull-right">总代金券金额{{total.limit_sum}}元 &nbsp;
                总抵扣金额{{total.discount_sum}}元&nbsp;
                总使用{{total.use_num_sum}}张</span>
        </div>
    </div>
</template>
<script>
    import {
        getList,
        getPage,
        getStoreList,
    } from "@/api/finance/voucher_detail.js";
    import { pickerOptionsF, pickerDateF, optionMaxTime } from '@/utils'
    export default {
        data() {
            return {
                list: null,
                listLoading: false,
                storeList: [],
                currentPage: 1,
                totalDataNumber: 0,
                total: {},
                formInline: {
                    store_id: '',
                    begin_time: '',
                    end_time: ''
                },
                pickerOptions: pickerOptionsF(),
                pickerDate: pickerDateF()
            }
        },
        created() {
            this.getStore()
        },
        methods: {
            getBrand() {
                getBrandList({ org: 1, category_id: 341 }).then(response => {
                    this.brandList = response;
                    this.formInline.brand = response[0].id
                    this.getStore()
                });
            },
            getStore() {
                getStoreList({ brand: this.formInline.brand, category_id: 341 }).then(response => {
                    this.storeList = response;
                  let data = response.filter(res => 
                        res.id == this.$store.getters.optionId
                    )
                    if(data.length>0){
                        this.formInline.store_id = data[0].id;
                    } else {
                        if(this.storeList.length > 0) {
                            this.formInline.store_id = this.storeList[0].id;
                        }
                    }
                    if (this.list == null) {
                        this.searchClick()
                    }
                });
            },
            // 搜索
            searchClick() {
                optionMaxTime(this.pickerDate).then(() => {//最大可选择时间区间
                    this.formInline.begin_time = this.pickerDate ? this.pickerDate[0] : '';
                    this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
                    this.fetchData();
                });
            },
            // 列表获取
            fetchData() {
                this.listLoading = true;
                this.currentPage=1;
                getPage(this.formInline).then(response => {
                    this.totalDataNumber = response.count * 1;
                    this.total.limit_sum = (response.limit_sum / 100).toFixed(2) * 1;
                    this.total.discount_sum = (response.discount_sum / 100).toFixed(2) * 1;
                    this.total.use_num_sum = response.use_num_sum;
                    getList(this.formInline).then(response => {
                        response.forEach((val, i) => {
                            val.discount_money = (val.discount_money / 100).toFixed(2) * 1;
                            val.limit_money = (val.limit_money / 100).toFixed(2) * 1;
                        });
                        this.list = response;
                        this.listLoading = false;
                    })
                }).catch(res=>{
                    this.listLoading=false;
                })
            },
            // 分页
            handleCurrentChange(val) {
                this.listLoading = true;
                getList(this.formInline, val).then(response => {
                    response.forEach((val, i) => {
                        val.discount_money = (val.discount_money / 100).toFixed(2) * 1;
                        val.limit_money = (val.limit_money / 100).toFixed(2) * 1;
                    });
                    this.list = response;
                    this.listLoading = false;
                });
            },
        }
    }
</script>